using System;
using System.Collections.Generic;
using EveryFunc;
using UnityEngine;
[RequireComponent(typeof(Animator))]
public class PlayerController : MonoBehaviour
{
    [Header("共有变量")]
    public float HP = 5;
    [Tooltip("默认移动速度")]
    public float moveSpeed;
    [Tooltip("跳跃高度")]
    public float jumpHeight;
    [Header("私有变量")]
    //当前移动速度
    public float m_speed;
    //跳跃速度
    private float jumpSpeed;
    private Animator animator;
    //移动方向
    private Vector2 moveDir;
    private Transform playerChildTF;
    private SpriteRenderer sprite;
    private Rigidbody2D rb;
    private bool isJump;
    private bool walkAble;
    //跳跃的虚拟z轴速度
    private float velocity_Y;
    private void Awake()
    {
        rb = this.GetComponent<Rigidbody2D>();
        animator = this.GetComponent<Animator>();
        playerChildTF = this.transform.Find("PlayerChild");
        sprite = playerChildTF.GetComponent<SpriteRenderer>();
    }
    private void Start()
    {
        //MouseManager.Instance.OnMouseClicked += MoveToTarget;
        m_speed = moveSpeed;
        isJump = false;
        walkAble = true;
    }
    private void Update()
    {
        //如果能够走路
        if (walkAble)
        {
            moveDir.x = Input.GetAxisRaw("Horizontal");
            moveDir.y = Input.GetAxisRaw("Vertical") * ConstantList.moveYPer;
            //如果跳跃 且当前不是跳跃状态
            /* if (Input.GetKeyDown(KeyCode.Space) && !isJump)
            {
                isJump = true;
                ReadyToJump();
            } */
        }
        //检测跳跃状态
        CheckJump();
    }

    private void CheckJump()
    {
        //判断子物体是在下落状态(velocity_Y<0)而且子物体离父物体距离小于等于0.05
        if (playerChildTF.position.y <= this.transform.position.y + 0.05f && velocity_Y < 0)
        {
            //满足了就说明跳跃完成
            velocity_Y = 0;
            playerChildTF.position = this.transform.position;
            isJump = false;
        }
    }

    private void FixedUpdate()
    {
        //移动
        Move();
        //跳跃
        Jump();
        //角色图片翻转
        PlayerClip();
    }
    
    /// 角色移动
    private void Move()
    {
        //50是一个缩进值：让m_speed不用那么大
        rb.velocity = moveDir.normalized * m_speed * Time.fixedDeltaTime * ConstantList.speedPer;
    }
    /// 角色跳跃
    private void Jump()
    {
        //重力模拟
        velocity_Y += ConstantList.g * Time.fixedDeltaTime;
        playerChildTF.Translate(new Vector3(0, velocity_Y) * Time.fixedDeltaTime);
    }
    /// 角色图片翻转
    public void PlayerClip()
    {
        if (moveDir.x >= 0.05f)
        {
            sprite.flipX = false;
        }
        else if (moveDir.x <= -0.05f)
        {
            sprite.flipX = true;
        }
    }
    /// 角色跳跃
    private void ReadyToJump()
    {
        velocity_Y = Mathf.Sqrt(jumpHeight * -2f * ConstantList.g);
    }
    /// 操控玩家跳跃
    public void PlayerJump()
    {
        if (!isJump)
        {
            isJump = true;
            ReadyToJump();
        }
    }
    /*     private void MoveToTarget (Vector3 target) {
            //TODO:获得路径，走向目标 应该不需要
            List<PathNode> pathList = GridManager.Instance.FindPath (this.transform.position, target);
            if (pathList.Count <= 1) return;
        }
     */
}